package com.kitty.game.player.serializer;

import com.kitty.game.role.model.Role;
import com.kitty.game.utils.ClassScanner;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class PlayerSerializerUtil {

    private static List<IPlayerPropSerializer> propSerializers = new ArrayList<>();

    private final static String SCAN_PATH = "com.kitty.game";

    static {
        Set<Class<?>> handler = ClassScanner.listAllSubclasses(SCAN_PATH, IPlayerPropSerializer.class);
        for (Class<?> clazz: handler) {
            try {
                propSerializers.add((IPlayerPropSerializer) clazz.newInstance());
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }

    public static void serialize(Role player) {
        for (IPlayerPropSerializer handler:propSerializers) {
            handler.serialize(player);
        }
    }

    public static void deserialize(Role player) {
        for (IPlayerPropSerializer handler:propSerializers) {
            handler.deserialize(player);
        }
    }
}
